package com.xique.door.service.impl;


import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.domain.PermissionGroupUser;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.mapper.PermissionGroupDeviceMapper;
import com.xique.door.mapper.PermissionGroupMapper;
import com.xique.door.mapper.PermissionGroupUserMapper;
import com.xique.door.mapper.UserDeviceMapper;
import com.xique.door.service.IPermissionGroupDeviceService;
import com.xique.door.service.IPermissionGroupDomainService;
import com.xique.door.service.IUserDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PermissionGroupDomainServiceImpl implements IPermissionGroupDomainService {

    @Autowired
    private PermissionGroupMapper permissionGroupMapper;

    @Autowired
    private UserDeviceMapper userDeviceMapper;

    @Autowired
    private PermissionGroupDeviceMapper permissionGroupDeviceMapper;

    @Autowired
    private PermissionGroupUserMapper permissionGroupUserMapper;

    @Autowired
    private IPermissionGroupDeviceService permissionGroupDeviceService;

    @Autowired
    private IUserDeviceService userDeviceService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int selectDevice(List<PermissionGroupDevice> devices, List<UserDevice> userDevices) {

        int count = permissionGroupMapper.batchDevices(devices);

        if (count <= 0) {
            return 0;
        }

        if (userDevices.isEmpty()) {
            return 1;
        }

        count = userDeviceMapper.batchInsertUserDevice(userDevices);

        if (count <= 0) {
            return 0;
        }

        return 1;
    }

    @Override
    public int deleteDevice(Long groupId, List<Long> deviceIds) {

        permissionGroupMapper.deleteDeviceInfos(groupId, deviceIds);

        userDeviceMapper.deleteUserDeviceByGroupIdAndDeviceIds(groupId, deviceIds);

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int selectUser(List<PermissionGroupUser> users, List<UserDevice> userDevices) {

        int count = permissionGroupMapper.batchUsers(users);

        if (count <= 0) {
            return 0;
        }

        if (userDevices.isEmpty()) {
            return 1;
        }

        count = userDeviceMapper.batchInsertUserDevice(userDevices);

        if (count <= 0) {
            return 0;
        }

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUser(Long groupId, List<Long> userIds) {

        permissionGroupMapper.deleteUsers(groupId, userIds);

        userDeviceMapper.deleteUserDeviceByGroupIdAndUserIds(groupId, userIds);

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteGroup(Long groupId) {

        permissionGroupMapper.deletePermissionGroupById(groupId);

        permissionGroupDeviceMapper.deletePermissionGroupDeviceByGroupId(groupId);

        permissionGroupUserMapper.deletePermissionGroupUserByGroupId(groupId);

        userDeviceMapper.deleteUserDeviceByGroupId(groupId);

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean addDevices(List<PermissionGroupDevice> devices, List<UserDevice> userDevices) {

        int count = permissionGroupDeviceService.batchInsertPermissionGroupDevice(devices);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchInsertUserDevice(userDevices);
        if (count <= 0) {
            return false;
        }

        return true;
    }
}
